// This MFC Samples source code demonstrates using MFC Microsoft Office Fluent User Interface 
// (the "Fluent UI") and is provided only as referential material to supplement the 
// Microsoft Foundation Classes Reference and related electronic documentation 
// included with the MFC C++ library software.  
// License terms to copy, use or distribute the Fluent UI are available separately.  
// To learn more about our Fluent UI licensing program, please visit 
// http://msdn.microsoft.com/officeui.
//
// Copyright (C) Microsoft Corporation
// All rights reserved.

// TopoViewerView.cpp : implementation of the CTopoViewerView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "TopoViewer.h"
#endif

#include "TopoViewerDoc.h"
#include "TopoViewerView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CTopoViewerView

IMPLEMENT_DYNCREATE(CTopoViewerView, CView)

BEGIN_MESSAGE_MAP(CTopoViewerView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CTopoViewerView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

BOOL CTopoViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CTopoViewerView drawing

void CTopoViewerView::OnDraw(CDC* pDC)
{
	CTopoViewerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CRect rectScreen;
	GetClientRect(rectScreen);
	/*rectScreen.left += 25;
	rectScreen.top += 25;
	rectScreen.right -= 25;
	rectScreen.bottom -= 25;*/

	pDC->IntersectClipRect(rectScreen);

	TpqMapManager::Instance().Draw(*pDC, rectScreen);
	//mapManager.DrawUTMGrids(*pDC, rectWorld, worldToScreen);
}


// CTopoViewerView printing


void CTopoViewerView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CTopoViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTopoViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTopoViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CTopoViewerView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CTopoViewerView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CTopoViewerView diagnostics

#ifdef _DEBUG
void CTopoViewerView::AssertValid() const
{
	CView::AssertValid();
}

void CTopoViewerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTopoViewerDoc* CTopoViewerView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTopoViewerDoc)));
	return (CTopoViewerDoc*)m_pDocument;
}
#endif //_DEBUG


// CTopoViewerView message handlers

int CTopoViewerView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	CPaintDC dc (this);

	mapManager.Initialize(dc, LatLongCoord(47.75*3600, -121.1*3600), -2560/450);
	return 0;
}

BOOL CTopoViewerView::OnEraseBkgnd(CDC* pDC)
{
	// Do not erase the background to reduce flickering
	return FALSE;
}


void CTopoViewerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	CDC* dc = GetDC();
	if (nChar == VK_DOWN)
	{
		mapManager.Pan(*dc, 0, 1);
		Invalidate(FALSE);
	}
	else if (nChar == VK_UP)
	{
		mapManager.Pan(*dc, 0, -1);
		Invalidate(FALSE);
	}
	else if (nChar == VK_LEFT)
	{
		mapManager.Pan(*dc, -1, 0);
		Invalidate(FALSE);
	}
	else if (nChar == VK_RIGHT)
	{
		mapManager.Pan(*dc, 1,0);
		Invalidate(FALSE);
	}
	
	if (nChar == VK_ADD)
	{
		mapManager.Zoom(1);
		Invalidate(FALSE);
	}
	else if (nChar == VK_SUBTRACT)
	{
		mapManager.Zoom(-1);
		Invalidate(FALSE);
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CTopoViewerView::OnIdle()
{
	mapManager.OnIdle(*this);
}

